<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Index extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('indexButton_m');
    }

    public function index()
    {
        $button         = $this->indexButton_m->select();
        $data['button'] = $button;
        $this->load->view('indexmenu/index', $data);
    }

    public function add()
    {
        $this->load->view('indexmenu/form');
    }

    public function edit($id = null)
    {
        $button = $this->indexButton_m->find($id);
        if (!$button) {
            show_message('参数错误！', site_url(X_URL . 'Index/index'));
        }
        $data['button'] = $button;
        $this->load->view('indexmenu/form', $data);
    }

    public function del()
    {

        $get     = $this->input->get();
        $type_id = $get['type_id'];
        if ($type_id) {
            if ($this->indexButton_m->delete($type_id)) {
                show_message('恭喜，删除成功！', site_url(X_URL . 'Index/index'), 1);
            } else {
                show_message('参数错误！', site_url(X_URL . 'Index/index'));
            }
        } else {
            $this->error('参数错误！');
        }
    }

    public function update()
    {
        $post              = $this->input->post();
        $data['title']     = $post['title'];
        $data['url']       = $post['url'] ?? "#";
        $data['create_at'] = date("Y-m-d H:i:s");
        $id                = $post['id'] ?? 0;
        if ($data['title'] == '') {
            show_message('名称不能为空！', site_url(X_URL . 'Index/index'));
        }
        if (!$id) {
            $this->indexButton_m->insert($data);
        } else {
            $this->indexButton_m->update(['id' => $id], $data);
        }
        show_message('恭喜，操作成功！', site_url(X_URL . 'Index/index'), 1);
    }
}

